var article_index_js = {
    psize: 10,
    pno: 1,
    layer: null,
    table: null,
    laydate: null,
    laypage: null,
    layerIndex: null,
    page_init: function (curnum, limitcount) {
        article_index_js.table.render({
            elem: '#article_index_table'
            , url: '/article/getByPage?pno=' + article_index_js.pno + '&psize=' + article_index_js.psize
            , cols: [
                [
                    {field: 'id', title: 'ID', width: '20%'}
                    , {field: 'title', title: '标题', width: '20%'}
                    , {field: 'postDate', title: '创建时间', width: '9%'}
                    , {field: 'source', title: '来源', width: '8%'}
                    , {field: 'endorse', title: '赞', width: '4%'}
                    , {field: 'browse', title: '评论', width: '4%'}
                    , {field: 'ranking', title: '推荐', width: '4%'}
                    , {field: 'state', title: '状态', width: '4%'}
                    , {field: 'avaterUrl', title: '标签', width: '12%', templet: '#article_index_titleTpl'}
                    , {field: 'poperation', title: '操作', width: '15%',fixed: 'right', toolbar: '#article_index_toolbar'}
                ]
            ]
            , page: false
            , height: 'full-350'
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                article_index_js.laypage.render({
                    elem: 'article_index_laypage'
                    , count: count
                    , limits: [10, 20]
                    , curr: curnum
                    , limit: limitcount
                    , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
                    , jump: function (obj, first) {
                        if (!first) {
                            article_index_js.pno = obj.curr;
                            article_index_js.psize = obj.limit;
                            article_index_js.page_init(article_index_js.pno, article_index_js.psize);
                        }
                    }
                })
            }
        });
    },
    btn_init: function () {
        article_index_js.table.on('tool(article_index_table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            //var tr = obj.tr; //获得当前行 tr 的DOM对象
            if(layEvent === 'article_index_delete'){ //删除
                article_index_js.layer.confirm('真的删除行么', function(index){
                    $.post('/article/delete', {id: data.id}, function (data,status) {
                        if (data.state == 'success'){
                            article_index_js.page_init(article_index_js.pno, article_index_js.psize);
                            article_index_js.layer.msg(data.message,{icon:6});
                        }else {
                            article_index_js.layer.msg(data.message,{icon:5});
                        }
                    });
                    article_index_js.layer.close(index);
                });
            } else if(layEvent === 'article_index_release'){ //删除
                article_index_js.layer.confirm('确定发布或撤回？', function(index){
                    $.post('/article/publish', {id: data.id}, function (data,status) {
                        if (data.state == 'success'){
                            article_index_js.page_init(article_index_js.pno, article_index_js.psize);
                            article_index_js.layer.msg(data.message,{icon:6});
                        }else {
                            article_index_js.layer.msg(data.message,{icon:5});
                        }
                    });
                    article_index_js.layer.close(index);
                });
            } else if(layEvent === 'article_index_update'){ //编辑
                window.open("/article/update?id=" + data.id);
            } else if (layEvent === 'article_index_info'){

            }
        });
        $('#article_index_add').on('click', function () {
            window.open("/article/add");
        });
    }
}

var article_add_js = {
    editor: null,
    layer: null,
    upload: null,
    form: null,
    content: null,
    init: function () {
        var E = window.wangEditor;
        article_add_js.editor = new E('#article_editor_menu','#article_add_editor');
        article_add_js.editor.customConfig.uploadImgServer = '/article/upload';
        article_add_js.editor.customConfig.uploadImgMaxSize = 10 * 1024 * 1024;
        article_add_js.editor.customConfig.uploadImgParams = {id: $('#article_id').attr('data-id')};
        article_add_js.editor.customConfig.uploadFileName = 'img';
        article_add_js.editor.create();

        var uploadInst = article_add_js.upload.render({
            elem: '#article_add_img' //绑定元素
            ,url: '/article/upload'//上传接口
            ,field: 'img'
            ,data: {id: $('#article_id').attr('data-id')}
            ,done: function(res){
                //上传完毕回调
                if (res.errno == '0'){
                    console.log(res.data[0]);
                    $('#article_id').attr('data-img',res.data[0]);
                    article_add_js.layer.msg('上传成功',{icon: 6});
                    $('#article_add_img_show').html('<img src="'+ res.data[0]+ '" />');
                }else {
                    article_add_js.layer.msg(res.data,{icon: 5});
                }
            }
            ,error: function(){
                //请求异常回调
                article_add_js.layer.msg('上传失败',{icon: 5});
            }
        });
    },
    btn_init: function () {
        $('#article_add_exit').on('click', function () {
            article_add_js.layer.confirm('真的关闭么', function(index){
                window.close();
                article_add_js.layer.close(index);
                article_index_js.page_init(article_index_js.pno, article_index_js.psize);
            });
        });
        $('#article_add_sure').on('click', function () {
            var _content = article_add_js.editor.txt.html();
            var _subContent = article_add_js.editor.txt.text();
            _subContent = _subContent.length > 200 ? _subContent.substring(0, 200) : _subContent;
            console.log(_subContent);
            var _tagsItem = $('input[type = checkbox][class = article_add_tag]:checked');
            var _tags = '';
            if (_tagsItem != null && _tagsItem != undefined){
                for (var i = 0; i < _tagsItem.length; i++){
                    _tags += (',' + $(_tagsItem[i]).attr('id'));
                }
                _tags = _tags.length > 1 ? _tags.substring(1) : '';
            }
            var data = {id: $('#article_id').attr('data-id'),
                    image: $('#article_id').attr('data-img'),
                    title: $('#article_title').val(),
                    source: $('#article_add_source').val(),
                    ranking: $('#article_add_ranking').val(),
                    content: _content,
                    subContent: _subContent,
                    tags: _tags};
            article_add_js.layer.confirm('真的提交么', function(index){
                $.post('/article/update', data, function (data,status) {
                    if (data.state == 'success'){
                        //article_index_js.page_init(article_index_js.pno, article_index_js.psize);
                        article_add_js.layer.msg(data.message,{icon:6});
                    }else {
                        article_add_js.layer.msg(data.message,{icon:5});
                    }
                });
                article_add_js.layer.close(index);
            });
        })
    }
}